#include "global.h"

struct PresetType *PresetPointer;

void InitRam (void)
{
};


/*
void InitTMR0 (void)
{
	OPTION=0;
	PSA=1;			// prescaller assigned to WDT
	T0CS = 0;		// Timer increments on instruction clock
	PS0=1;			// PRESCALLER 1:8 t0   PRESCALLER 1:32 wdt
	PS1=1;			//  tick = 409.6 mks per 20mHz clock 
	PS2=1;			//  tick = 2048 mks per 4mHz clock
	T0IF=0;			//
	T0IE=0;			// enable T0 interrupt
};
*/

void InitTMR1 (void) 	// i want a 0.005 sec interrupt  i have a 1us - so i need count up to 5000us or 5000 with 1:1 prescaller
{	T1CON = 0b00000001; //1:1 PRESCALLER, TIMER ON
	TMR1IF=0;
	TMR1IE=1;		// enable T1 interrupt
	PEIE=1;			// enable peripherial interrupt
};

void TMR1Preload (unsigned int value)
{	unsigned char lbyte, hbyte;
	lbyte=(char)value ;
	hbyte=(char)(value >> 8);
	TMR1H=hbyte;		//
	TMR1L=lbyte;		//
};

unsigned char ButtonRead(void)
{	static unsigned char mask,vcount0,vcount1,prevstate;
	unsigned char temp, scan, temptris;
	temptris=TRISB;
	PORTB=0xff;
	scan=0;
	TRISB=0b11110000;
	RBPU=0;
 
	PORTB=0b11111110;
	temp =PORTB;
	if (!(temp&0b00010000)) scan|=0b00000001;
	if (!(temp&0b00100000)) scan|=0b00010000;

	PORTB=0b11111101;
	temp =PORTB;
	if (!(temp&0b00010000)) scan|=0b00000010;
	if (!(temp&0b00100000)) scan|=0b00100000;

	PORTB=0b11111011;
	temp =PORTB;
	if (!(temp&0b00010000)) scan|=0b00000100;
	if (!(temp&0b00100000)) scan|=0b01000000;

	PORTB=0b11110111;
	temp =PORTB;
	if (!(temp&0b00010000)) scan|=0b00001000;
	if (!(temp&0b00100000)) scan|=0b10000000;
	
//	scan^=0xff;
	mask=scan^prevstate;

	vcount0&=mask;
	vcount1&=mask;
	prevstate^ =( mask&vcount0&vcount1);
	vcount0^=mask;
	vcount1^=(vcount0^mask);


	TRISB=temptris;
	return prevstate;
};

unsigned char time[9];
unsigned char* ShowLTime(unsigned int seconds,unsigned char* tbuffer)  //00:00:00
{	int st_temp;
	const int SECSINTENHOURS=36000;
	const int SECSINHOUR=3600;
	const int SECSINTENMINUTES=600;
	const int SECSINMINUTE=60;
	unsigned char digit=0;

	while (seconds >= SECSINTENHOURS)
	{	seconds-=SECSINTENHOURS;
		digit++;
	};
	
		tbuffer[0]=0x30+digit;
		digit=0;
	
	while (seconds >= SECSINHOUR)
	{	seconds-=SECSINHOUR;
		digit++;
	};
		tbuffer[1]=0x30+digit;
		digit=0;

	tbuffer[2]=':';
	while (seconds >= SECSINTENMINUTES)
	{	seconds-=SECSINTENMINUTES;
		digit++;
	};
	
		tbuffer[3]=0x30+digit;
		digit=0;
	
	
	while (seconds >= SECSINMINUTE)
	{	seconds-=SECSINMINUTE;
		digit++;
	};
	
		tbuffer[4]=0x30+digit;
		digit=0;
	tbuffer[5]=':';

	while (seconds >9)
	{	seconds-=10;
		digit++;
	};
		tbuffer[6]=0x30+digit;
		digit=0;

	tbuffer[7]=0x30+seconds;
	tbuffer[8]=0;
	return tbuffer;
}



unsigned char* ShowSTime(unsigned int seconds,unsigned char* tbuffer) // 000:00
{	int st_temp;

	const int SECSINHUNDREDMINUTES=6000;
	const int SECSINTENMINUTES=600;
	const int SECSINMINUTE=60;
	unsigned char digit=0;

	if (seconds >= SECSINHUNDREDMINUTES)
	{	while (seconds >= SECSINHUNDREDMINUTES)
		{	seconds-=SECSINHUNDREDMINUTES;
			digit++;
		};
		tbuffer[0]=0x30+digit;
		digit=0;
	}
	else
	tbuffer[0]=' ';		
	while (seconds >= SECSINTENMINUTES)
	{	seconds-=SECSINTENMINUTES;
		digit++;
	};
	
		tbuffer[1]=0x30+digit;
		digit=0;
	
	
	while (seconds >= SECSINMINUTE)
	{	seconds-=SECSINMINUTE;
		digit++;
	};
	
		tbuffer[2]=0x30+digit;
		digit=0;
	tbuffer[3]=':';

	while (seconds >9)
	{	seconds-=10;
		digit++;
	};
		tbuffer[4]=0x30+digit;
		digit=0;

	tbuffer[5]=0x30+seconds;
	tbuffer[6]=tbuffer[7]=tbuffer[8]=0;
	return tbuffer;
}

unsigned char* ShowCurrent(unsigned char Current,unsigned char* tbuffer) // 000:00
{	unsigned char sc_temp=0;
	unsigned int sc_int=0;
	sc_int=Current;
	sc_int*=2;
	if (sc_int >=100 )
	{	while (sc_int>=100)
		{	sc_int-=100;
			sc_temp+=1;
		};
		tbuffer[0]=0x30+sc_temp;
		sc_temp=0;
	}
	else
	{	tbuffer[0]=' ';
	};

	while (sc_int>=10)
		{	sc_int-=10;
			sc_temp+=1;
		};
		tbuffer[1]=0x30+sc_temp;
		sc_temp=0;

		tbuffer[2]='.';
		tbuffer[3]=0x30+sc_int;
		tbuffer[4]=tbuffer[5]=0;
	return tbuffer;
}


void Critical_stop(unsigned char* err_string)
{
	//  error message displaying
	ClearLCD();  
	CursorPosLCD(1,1);
	printf(err_string);
	CursorPosLCD(2,1);
	printf("Test Halted");
	
	// preparing ITU packet for interrupt transfer
	MyITU.Data.OOprEEV  = 150;
	MyITU.Data.CompFreq = 0;   //stop compressor
	MyITU.Data.OOprOFan = 0;   //  stop fan
	MyITU.Data.IOprMMode = 0;  //  Idle mode (STBY)

	while(1)
	{
		ClearWDT();
	};

}






